package com.linx.common.web.utils;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.linx.common.core.utils.HttpClientUtils;
import com.linx.common.core.utils.SpringUtils;
import com.linx.common.web.domain.bo.DeleteRequest;
import com.linx.common.web.domain.bo.PersonBatchAdd;
import com.linx.common.web.domain.bo.PersonBatchDelete;
import com.linx.common.web.domain.vo.*;
import com.linx.common.web.properties.HkProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * @ClassName PersonBatchAddControlUtils
 * @Description 海康人员操作
 * @Author gushaoliang
 * @Date 2024/12/4 下午4:43
 * @Version 1.0
 */
@Component
@Slf4j
public class PersonBatchControlUtils {
    private String agreement;
    private String host;
    private String appKey;
    private String appSecret;
    private String artemisPath;


    public PersonBatchControlUtils() {
        HkProperties hkProperties = SpringUtils.getBean(HkProperties.class);
        agreement = hkProperties.getAgreement();
        host = hkProperties.getHost();
        appKey = hkProperties.getAppKey();
        appSecret = hkProperties.getAppSecret();
        artemisPath = hkProperties.getArtemisPath();
    }

//批量添加人员
    public  ResponseMultipleVo add(List<PersonBatchAdd> addList) throws Exception {
        String apiPath = artemisPath +"/api/resource/v1/person/batch/add";
        Map<String, String> path = new HashMap<>(2);

        path.put(agreement + "://", artemisPath + apiPath);
        String body=JSON.toJSONString(addList);
        String json = ArtemisHttpUtil.doPostStringArtemis( path, body, null,  null, "application/json");

        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json：" + jsonObject);

        ResponseMultipleVo<SuccessVo, FailureVo> responseVo = JSON.parseObject(json, new TypeReference<ResponseMultipleVo<SuccessVo, FailureVo>>() {});

        return responseVo;
    }

    //批量删除人员
    public  ResponseVo delete(PersonBatchDelete personBatchDelete) throws Exception {
        String apiPath = artemisPath +"/api/resource/v1/person/batch/delete";
        Map<String, String> path = new HashMap<>(2);

        path.put(agreement + "://", artemisPath + apiPath);
        String body=JSON.toJSONString(personBatchDelete);

        String json = ArtemisHttpUtil.doPostStringArtemis( path, body, null,  null, "application/json");

        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json：" + jsonObject);

        ResponseVo<List<PersonBatchDeleteVo>> response = JSON.parseObject(
            json,
            new TypeReference<ResponseVo<List<PersonBatchDeleteVo>>>() {}
        );
        return response;
    }


    //新增人员信息V2
    public  ResponseVo addPersonV2(PersonBatchAdd personBatchAdd) throws Exception {
        String apiPath = artemisPath +"/api/resource/v2/person/single/add";
        Map<String, String> path = new HashMap<>(2);

        path.put(agreement + "://", apiPath);
        String body=JSON.toJSONString(personBatchAdd);
        ArtemisConfig.host=host;
        ArtemisConfig.appKey=appKey;
        ArtemisConfig.appSecret=appSecret;
        log.info("请求body:{}", body);
        String json = ArtemisHttpUtil.doPostStringArtemis( path, body, null,  null, "application/json");
        JSONObject jsonObject = JSONObject.parseObject(json);
        log.info("返回的json：" + jsonObject);

        ResponseVo response = JSON.parseObject(
            json,
            new TypeReference<ResponseVo<ResponseMultipleVo<SuccessVo, FailureVo>>>() {}
        );
        return response;
    }



        public static void main(String[] args) {
            // JSON字符串
            String jsonStr = "{\n" +
                "    \"code\": \"0\",\n" +
                "    \"msg\": \"success\",\n" +
                "    \"data\": {\n" +
                "        \"successes\": [\n" +
                "            {\n" +
                "                \"clientId\": 123456789,\n" +
                "                \"personId\": \"5668f44c-ae23-4674-9b79-d8f79f77fd41\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"failures\": [\n" +
                "            {\n" +
                "                \"clientId\": 555,\n" +
                "                \"code\": \"-1\",\n" +
                "                \"message\": \"OrgIndexCode Not Exists\"\n" +
                "            }\n" +
                "        ]\n" +
                "    }\n" +
                "}";

            // 使用FastJSON解析
            ResponseMultipleVo<SuccessVo, FailureVo> responseVo = JSON.parseObject(jsonStr, new TypeReference<ResponseMultipleVo<SuccessVo, FailureVo>>() {});

            // 打印解析后的结果
            System.out.println(responseVo);
        }
    }

